<template>
  <div>
    <base-table :tableSetting="baseTableSetting"></base-table>
    <dialogPublic
      :title="title"
      :show.sync="show"
      v-if="show"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <editForm
        :formConfig="formConfig"
        ref="editForm"
        :formData="editFormData"
      ></editForm>
    </dialogPublic>
  </div>
</template>

<script>
import {
  queryApprovalRecord,
  delaySubmit,
} from "@/api/orderManagement/overhaulApplication.js";
export default {
  props: {
    allInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      show: false,
      title: "",
      minTime: null,
      maxTime: null,
      baseTableSetting: {
        indexShow: true,
        noBorderRight: false,
        column: [
          {
            label: "延时申请",
            textAlign: "center",
            subColumn: [
              {
                label: "申请延长结束时间",
                prop: "delayTime",
                textAlign: "center",
              },
              {
                label: "申请人",
                prop: "createByName",
                textAlign: "center",
              },
              {
                label: "申请时间",
                prop: "createTime",
                textAlign: "center",
              },
            ],
          },
          {
            label: "延时批复",
            textAlign: "center",
            subColumn: [
              {
                label: "批复延长结束时间",
                prop: "endTime",
                textAlign: "center",
              },
              {
                label: "批复人",
                prop: "updateByName",
                textAlign: "center",
              },
              {
                label: "批复时间",
                prop: "createTime",
                textAlign: "center",
              },
              {
                label: "批复结果",
                prop: "result",
                textAlign: "center",
                formatter: (row) => {
                  if (row.result) {
                    return row.result == 1 ? "同意" : "不同意";
                  } else {
                    return "-";
                  }
                },
              },
              {
                label: "批复意见",
                prop: "opinion",
                textAlign: "center",
              },
            ],
          },
          {
            label: "状态",
            prop: "status",
            textAlign: "center",
            formatter: (row) => {
              return row.status == 1 ? "已批复" : "未批复";
            },
          },
          {
            label: "操作",
            textAlign: "center",
            type: "button",
            buttonArr: [
              {
                text: "批复",
                content: "未授权功能",
                disabled: !this.$store.getters.isBtnAuth("reply"),
                isShow: (row) => {
                  return row.status != 1;
                },
                methods: (row, index) => {
                  this.approval(row);
                },
              },
            ],
          },
        ],
        data: [],
        noBorderRight: true,
        autoHeight: false,
        maxHeight: "220px",
        rowIndex: 0,
      },
      formConfig: {
        labelWidth: "150px",
        itemArr: [
          {
            label: "批复延长结束时间",
            prop: "endTime",
            type: "datetime",
            span: 13,
            required: true,
            rules: {
              trigger: "blur",
              validator: (rule, value, callback) => {
                this.checkTime(rule, value, callback);
              },
            },
            pickerOptions: {
              disabledDate: (time) => {
                return (
                  time.getTime() <= this.minTime ||
                  time.getTime() >= this.maxTime
                );
              },
            },
          },
          {
            label: "批复人",
            prop: "updateByName",
            type: "text",
            span: 13,
            required: true,
            disabled: true,
          },
          {
            label: "批复结果",
            prop: "result",
            type: "select",
            span: 13,
            required: true,
            selectOption: [
              { label: "同意", value: 1 },
              { label: "不同意", value: 0 },
            ],
          },
          {
            label: "批复意见",
            prop: "opinion",
            type: "textarea",
            span: 20,
            rows: 6,
            required: true,
          },
        ],
      },
      editFormData: {},
      currentRow: {},
    };
  },
  created() {
    this.$set(this.baseTableSetting, "data", this.allInfo.applicationDelayList);
  },
  watch: {
    allInfo: {
      handler(val) {
        this.$set(this.baseTableSetting, "data", val.applicationDelayList);
      },
      deep: true,
    },
  },
  methods: {
    //校验申请时间
    checkTime(rule, val, callback) {
      let minTime = new Date(this.allInfo.applicationCheck.endTime).getTime();
      let maxTime = new Date(this.currentRow.delayTime).getTime();
      let time = new Date(val).getTime();
      let message = `请在${this.allInfo.applicationCheck.endTime}-${this.currentRow.delayTime}内选择`;
      if (minTime >= time && time > maxTime) {
        //debugger
        return callback(new Error(message));
      } else {
        callback();
      }
    },
    //获取记录列表
    getRecordList() {
      queryApprovalRecord({ id: this.id }).then((res) => {
        if (res.code == 200) {
          this.$set(this.baseTableSetting, "data", res.data);
        }
      });
    },
    //打开批复弹窗
    approval(row) {
      this.currentRow = row;
      this.editFormData = row;
      this.editFormData.endTime = row.delayTime;
      this.minTime = new Date(
        `${this.allInfo.applicationCheck.endTime.split(" ")[0]} 00:00`
      ).getTime();
      this.maxTime = new Date(`${row.delayTime.split(" ")[0]} 24:00`).getTime();
      this.editFormData.updateByName = this.$store.getters.userInfo.name;
      this.show = true;
    },
    //批复申请
    async doSubmit() {
      let formData = await this.$refs.editForm.getFormData();
      delaySubmit(formData).then((res) => {
        if (res.code == 200) {
          this.$utils.addOperateLog(this.allInfo.id, "批复了延时申请");
          this.$message.success(res.description);
          this.$emit("doApproval");
          this.show = false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>